package com.xteam.youin_supplier.fragment;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.adpter.TenderListViewAdapter;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BaseModel;
import com.xteam.youin_supplier.model.BeddingListItem;
import com.xteam.youin_supplier.util.http.AjaxCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.json.JSONObject;


@EFragment(R.layout.fragment_home)
public class HomeFragment extends BaseFragment {


    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;
    @ViewById(R.id.recyclerview)
    RecyclerView recyclerView;

    TenderListViewAdapter tenderListViewAdapter;
    HomeFragment homeFragment;

    @AfterViews
    @Override
    void initView() {
        homeFragment = this;
        backBtn.setVisibility(View.GONE);
        topTitle.setText(getResources().getString(R.string.bidding_list_title));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        loadData();
//        json();
    }


//    {
//        "data":
//        {
//            "list":[
//            {
//                "gasstation_id":3,"name":"重庆站2站","qty":"200L/1200元"
//            }
//            ,
//            {
//                "gasstation_id":2,"name":"上海1站","qty":"100L/600元"
//            }
//            ], "today_qtys":"300L","sums":"1800元"
//        }
//        ,"flag":true,"msg":"成功"
//    }


    //加载数据
    private void loadData() {
        requestUrl = RequestAddr.biddingList;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                dealData(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    //处理数据
    BeddingListItem beddingListItem;
    private void dealData(Object object) {
        try {
            beddingListItem = gson.fromJson(object.toString(), BeddingListItem.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }

        tenderListViewAdapter = new TenderListViewAdapter(getActivity().getApplicationContext(), beddingListItem);
        tenderListViewAdapter.setOnItemClickListener(new TenderListViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                MainActivity.getInstance().switchFragment(homeFragment,
                        TenderDetailFragment_.builder()
                                .auction_id(beddingListItem.getData().get(position).getAuction_id())
                                .build());

            }
        });
        recyclerView.setAdapter(tenderListViewAdapter);
    }

    @Override
    void Click(View view) {

    }


}
